package com.yiwei.jmx.view;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.activity.WechatChatActivity;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.QuestionFeedbackBean;
import com.yiwei.jmx.api.bean.XueXinQuestionDetailBean;
import com.yiwei.jmx.api.bean.XueXinQuestionUseBean;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/3/16 0016.
 */
public class WechatQuestionDialog extends Dialog {


    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.txt_content)
    TextView txtContent;
    @BindView(R.id.img_use)
    ImageView imgUse;
    @BindView(R.id.txt_use)
    TextView txtUse;
    @BindView(R.id.img_unuse)
    ImageView imgUnuse;
    @BindView(R.id.txt_unuse)
    TextView txtUnuse;
    @BindView(R.id.loading)
    CircularProgress loading;
    private Context context;
    private String id;
    private String title;
    private QuestionUnUseDilaog questionUnUseDilaog;
    private SelectMenuTipsDilaog selectMenuDialog;

    public WechatQuestionDialog(Context context) {
        super(context, R.style.mydialog);
        this.context = context;
        init();
    }

    public WechatQuestionDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public WechatQuestionDialog setOnSelect(OnSureListener listener) {
        selectListener = listener;
        return this;
    }

    public void setId(String id, String title) {
        this.id = id;
        this.title = title;
    }

    @Override
    public void show() {
        super.show();
        getXueXinQuestionList(id, title);
    }

    public void getXueXinQuestionFeedBack(String qid, String id) {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }
        loading.setVisibility(View.VISIBLE);
        QuestionFeedbackBean bean = new QuestionFeedbackBean();
        bean.qid = qid;
        bean.id = id;
        Api.api_service.getXueXinQuestionFeedBack(bean).compose(RxLifeUtil.checkOn(context)).
                doFinally(() -> {
                    try {
                        loading.setVisibility(View.GONE);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {

                });
    }

    public void getXueXinQuestionList(String id, String title) {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }
        loading.setVisibility(View.VISIBLE);
        XueXinQuestionDetailBean bean = new XueXinQuestionDetailBean();
        bean.qid = id;

        Api.api_service.getXueXinQuestionDetail(bean).compose(RxLifeUtil.checkOn(context)).
                doFinally(() -> {
                    try {
                        loading.setVisibility(View.GONE);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    txtTitle.setText(result.data.title);
                    txtContent.setText(result.data.content);
                });
    }

    public void getXueXinQuestionUse(String id, String use) {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }
        loading.setVisibility(View.VISIBLE);
        XueXinQuestionUseBean bean = new XueXinQuestionUseBean();
        bean.qid = id;
        bean.is_use = use;
        Api.api_service.getXueXinQuestionUse(bean).compose(RxLifeUtil.NocheckOn(context)).
                doFinally(() -> {
                    try {
                        loading.setVisibility(View.GONE);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    imgUnuse.setImageResource(R.mipmap.img_wechat_unuse_grey);
                    txtUnuse.setTextColor(0xffCCCCCC);
                    imgUse.setImageResource(R.mipmap.img_wechat_useful_grey);
                    txtUse.setTextColor(0xffCCCCCC);
                    if (use.equals("0")) {
                        imgUnuse.setImageResource(R.mipmap.img_wechat_unuse);
                        txtUnuse.setTextColor(0xff0072FF);
                    } else {
                        imgUse.setImageResource(R.mipmap.img_wechat_useful);
                        txtUse.setTextColor(0xff0072FF);
                    }
                    if (result.getStatus() == 1) {
                        if (use.equals("0")) {
                            if (result.data.list != null && result.data.list.size() != 0) {
                                questionUnUseDilaog.setData(result.data);
                                questionUnUseDilaog.show();
                            }
                        }
                    } else {
                        selectMenuDialog.show();
                    }
                });
    }

    @OnClick({R.id.btn_close, R.id.img_use, R.id.txt_use, R.id.img_unuse, R.id.txt_unuse})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                dismiss();
                break;
            case R.id.img_use:
            case R.id.txt_use:
                if (TextUtils.isEmpty(id)) {
                    return;
                }
                getXueXinQuestionUse(id, "1");
                if (selectListener != null) {
                    selectListener.onUse("");
                }
                break;
            case R.id.img_unuse:
            case R.id.txt_unuse:
                if (TextUtils.isEmpty(id)) {
                    return;
                }
                getXueXinQuestionUse(id, "0");
                selectListener.onNotUse("");
                break;
        }
    }

    public void setData(String text) {
        txtContent.setText(text);
    }

    public interface OnSureListener {
        void onUse(String id);

        void onNotUse(String id);
    }

    private OnSureListener selectListener;

    private void init() {
        Window window = this.getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setWindowAnimations(R.style.mydialog_animation);
        setContentView(R.layout.dialog_wechat_question_answer);
        ButterKnife.bind(this);

        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels;
        }
        window.setAttributes(lp);
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        this.setCanceledOnTouchOutside(false);

        txtContent.setMovementMethod(new ScrollingMovementMethod());

        questionUnUseDilaog = new QuestionUnUseDilaog(context);
        questionUnUseDilaog.setOnChooseListener(new QuestionUnUseDilaog.OnChooseListener() {
            @Override
            public void onSuccess(String qid, String id) {
                getXueXinQuestionFeedBack(qid, id);
                questionUnUseDilaog.dismiss();
            }
        });
        selectMenuDialog = new SelectMenuTipsDilaog(context).setOnChooseListener(new SelectMenuTipsDilaog.OnChooseListener() {
            @Override
            public void onLeft() {
                selectMenuDialog.dismiss();
            }

            @Override
            public void onRight() {
                Util.startActivity(context, WechatChatActivity.class);
                selectMenuDialog.dismiss();
            }
        }).showTwoSelectData("主人", R.mipmap.img_idcard_fail_dialog, "非常抱歉，为了更好解决这个问题，我\n" +
                "将邀请客服人员为您继续解答", "暂时不用", "联系客服");
    }

}
